package com.fjrz.bbxwj.main.video

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ColorUtils
import com.module.lemlin.mode.ViewModeActivity
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.VideoDetails
import com.fjrz.bbxwj.entity.VideoDraft
import com.fjrz.bbxwj.entity.VideoRequest
import com.fjrz.bbxwj.mode.vm.VideoViewModel
import com.module.lemlin.view.LoginAuthPopup
import com.tbruyelle.rxpermissions3.RxPermissions
import kotlinx.android.synthetic.main.activity_video_editing.*

class VideoEditingActivity : ViewModeActivity<VideoViewModel>() {

    companion object {

        private const val EXTRA_KEY_VIDEO_PATH = "EXTRA_KEY_VIDEO_PATH"
        private const val EXTRA_KEY_BITMAP_PATH = "EXTRA_KEY_BITMAP_PATH"
        private const val EXTRA_KEY_VIDEO_ACTION = "EXTRA_KEY_VIDEO_ACTION"
        private const val EXTRA_KEY_VIDEO_DRAFT = "EXTRA_KEY_VIDEO_DRAFT"

        private const val EDIT_YES = 1
        private const val EDIT_NOT = 2

        fun openEditing(activity: Activity, pathVideo: String, pathBitmap: String) {
            val intent = Intent(activity, VideoEditingActivity::class.java)
            intent.putExtra(EXTRA_KEY_VIDEO_PATH, pathVideo)
            intent.putExtra(EXTRA_KEY_BITMAP_PATH, pathBitmap)
            intent.putExtra(EXTRA_KEY_VIDEO_ACTION, EDIT_YES)
            activity.startActivity(intent)
        }

        fun openModify(activity: Activity, videoDraft: VideoDraft) {
            val intent = Intent(activity, VideoEditingActivity::class.java)
            intent.putExtra(EXTRA_KEY_VIDEO_ACTION, EDIT_NOT)
            intent.putExtra(EXTRA_KEY_VIDEO_DRAFT, videoDraft)
            activity.startActivity(intent)
        }
    }

    override fun viewModel(): VideoViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_video_editing

    override fun stateBar(): StatusBar =
        StatusBar(view = flVideoEditingBar)

    override fun initView() {
        flVideoEditingBody.addView(createViewEmptyLoad())

        ivVideoEditingBack.setOnClickListener {
            finish()
        }
    }

    override fun initData() {
        val rxPermission = RxPermissions(this)
        rxPermission.request(
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION
        ).subscribe { permission ->
            if (!permission) {
                toast(message = "请到设置中开启位置权限")
            } else {
                viewModel.videoCateTotalList()
            }
        }
    }

    override fun initViewMode() {

        viewModel.videoCateTotalListLiveData.observe(this as LifecycleOwner, Observer { response ->
            if (response.status == Status.START) {
                flVideoEditingBody.removeAllViews()
                flVideoEditingBody.addView(createViewEmptyLoad())
            } else if (response.status == Status.FAILURE) {
                flVideoEditingBody.removeAllViews()
                flVideoEditingBody.addView(
                    createViewEmptyNull(hint = response.throwable?.msg ?: "未知错误")
                )
            }
            if (response.status != Status.SUCCESS)
                return@Observer
            val action: Int = intent?.getIntExtra(EXTRA_KEY_VIDEO_ACTION, (0))
                ?: return@Observer
            if (action == EDIT_YES) {
                flVideoEditingBody.removeAllViews()
                val videoRequest = VideoRequest(
                    action = EDIT_YES,
                    path_bitmap = intent?.getStringExtra(EXTRA_KEY_BITMAP_PATH),
                    path_video = intent?.getStringExtra(EXTRA_KEY_VIDEO_PATH)
                )
                supportFragmentManager(VideoEditingFragment.newInstance(videoRequest))
            } else if (action == EDIT_NOT) {
                val videoDraft: VideoDraft =
                    intent?.getSerializableExtra(EXTRA_KEY_VIDEO_DRAFT) as? VideoDraft
                        ?: return@Observer
                viewModel.videoDetail(videoDraft.id)
            } else {
                flVideoEditingBody.removeAllViews()
                flVideoEditingBody.addView(createViewEmptyNull(hint = "未知错误！"))
            }
        })

        viewModel.videoDetailsLiveData.observe(this as LifecycleOwner, Observer { response ->
            if (response.status == Status.FAILURE) {
                flVideoEditingBody.removeAllViews()
                flVideoEditingBody.addView(
                    createViewEmptyNull(hint = response.throwable?.msg ?: "未知错误")
                )
            }
            if (response.status != Status.SUCCESS)
                return@Observer
            flVideoEditingBody.removeAllViews()
            val videoDetails: VideoDetails? = response.httpData?.data
            if (videoDetails == null) {
                flVideoEditingBody.addView(createViewEmptyNull(hint = "视频详情数据为空"))
            } else {
                val videoRequest = VideoRequest(
                    video_id = videoDetails.id.toString(), cate_id = videoDetails.cate_id,
//                    title = videoDetails.title,
                    description = videoDetails.description,
                    main_image = videoDetails.main_image, path = videoDetails.path,
                    province = videoDetails.province, city = videoDetails.city,
                    type = videoDetails.type, adv_image = videoDetails.adv_image,
                    adv_title = videoDetails.adv_title, action = EDIT_NOT
                )
                supportFragmentManager(VideoEditingFragment.newInstance(videoRequest))
            }
        })
    }

    private fun supportFragmentManager(fragment: Fragment) {
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.flVideoEditingBody, fragment)
        transaction.commit()
    }


    override fun onEventMainThread(any: Any) {
        if (any !is LoginAuthPopup.Action)
            return
        if (any == LoginAuthPopup.Action.ACTION_LOGIN_SUCCESS) {
            initData()
        }
    }
}